﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Common;
using NHibernate.Criterion;
using SwyAdmin;
namespace Web.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        //可以指点让某个用户访问,或者某个组的用户访问
        //[Authorize(Users = "?")]  
        public ActionResult Index()
        {
          
            ViewData["manager"] = new DAO.BLL.B_user().Get(Convert.ToInt32(User.Identity.Name));
            return View();
            
        }

        public ActionResult Login()
        {
            FormsAuthentication.SignOut();//安全退出
            return View();
        }

        [HttpPost]
        public JsonResult EditManagerPwd(string id, string txt_password)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_user b_manager = new DAO.BLL.B_user();
            var m_manager = b_manager.Get(Convert.ToInt32(id));
            if (m_manager.password != txt_password)
            {
                m_manager.password = txt_password;
            }
            b_manager.Update(m_manager);
            json.msg = "修改成功!";
            return Json(json);
        }
        public ActionResult Error()
        {
            return View();
        }
        public ActionResult Home()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        #region ===================内部方法==========================
        /// <summary>
        /// 普通登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userpwd"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult login(string un, string pwd, string code, string returnUrl)
        {
            Common.Json json = new Common.Json();
            if (Common.Encrypt.md5(code.ToLower()) != WebHelper.GetSession("lsj_pic_code"))
            {
                json.msg = "验证码不正确！";
                json.status = -1;
                return Json(json);
            }
            DAO.BLL.B_user b_manager = new DAO.BLL.B_user();
            if (ModelState.IsValid)
            {
                List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="username",value=un,searchType=Common.EnumBase.SearchType.Eq},
                    new SearchTemplate(){key="password",value=pwd,searchType=Common.EnumBase.SearchType.Eq}
                };
                var list_model = b_manager.GetList(st, null);
                if (list_model.Count > 0)
                {
                    FormsAuthentication.SetAuthCookie(list_model[0].ID.ToString(), true);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        json.returnUrl = returnUrl;
                    }
                    else
                    {
                        json.returnUrl = "/home/index";
                    }

                    //DAO.BLL.B_syslog b_log = new DAO.BLL.B_syslog();
                    ////记录用户所在地区
                    //var userip = Utils.getIp();
                    //string location = "";
                    //if (userip.Contains("."))
                    //{
                    //    location = Hui.Utils.IPHelper.GetFullName(userip);
                    //}

                    //b_log.Add(list_model[0].id, Common.EnumBase.Authorize.登录.Description(), "manager", "用户登录：" + location, Utils.getIp());
                }
                else
                {
                    json.msg = "用户名或密码不正确！";
                    json.status = -1;
                }
            }

            return Json(json);
        }

        #endregion ===================内部方法==========================



        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }

        public JsonResult CheckLogin()
        {
            Common.Json json = new Json();
            if (User.Identity.IsAuthenticated)
            {
                json.msg = "有效";
                json.status = 0;
            }
            else
            {
                json.status = -1;
                json.msg = "登录失效";

            }
            return Json(json);
        }


    }
}
